package com.cvmars.jianyou.module.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cvmars.jianyou.R;
import com.cvmars.jianyou.model.TaskModel;
import com.cvmars.jianyou.module.activity.TaskDetailActivity;
import com.cvmars.jianyou.module.adapter.HomeAdapter;
import com.cvmars.jianyou.module.adapter.HomeTaskTypeAdapter;
import com.cvmars.jianyou.module.base.BaseFragment;
import com.cvmars.jianyou.module.model.FindModel;
import com.cvmars.jianyou.ui.pulltorefresh.PulltoRefreshRecyclerView;
import com.cvmars.jianyou.utils.DensityUtils;
import com.cvmars.jianyou.utils.GlideImageLoader;
import com.gigamole.infinitecycleviewpager.HorizontalInfiniteCycleViewPager;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hehaifeng on 2018/11/26.
 */

public class HomeFragment extends BaseFragment {


    PulltoRefreshRecyclerView listHome;

    HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.frg_home_index, null);

        listHome = view.findViewById(R.id.list_home);


        List<FindModel> modelList = new ArrayList<>();

        homeAdapter = new HomeAdapter(getActivity(),R.layout.item_task, modelList);
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


                Intent intent = new Intent(getActivity(), TaskDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });
        View headerView = LayoutInflater.from(getContext()).inflate(R.layout.header_home, null);

        RecyclerView listTaskType = headerView.findViewById(R.id.list_task_type);
        Banner banner = headerView.findViewById(R.id.viewpager_ad);

        banner.getLayoutParams().height = DensityUtils.getWindowWidth(getActivity()) * 9 / 16;
        List<String> images = new ArrayList<>();

        images.add("https://gallery.youxiake.com/Public/Data/upload/201811/15/84661542271799.jpg?imageView2\\/0\\/q\\/90\\/w\\/800");
        images.add("https://gallery.youxiake.com/Public/Data/upload/201811/15/84661542271799.jpg?imageView2\\/0\\/q\\/90\\/w\\/800");
        images.add("https://gallery.youxiake.com/Public/Data/upload/201811/15/84661542271799.jpg?imageView2\\/0\\/q\\/90\\/w\\/800");
        banner.setImages(images).setImageLoader(new GlideImageLoader()).start();
        RecyclerView ad = headerView.findViewById(R.id.list_task_ad);
        ad.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        listTaskType.setLayoutManager(new GridLayoutManager(getContext(), 4));

        List<String> type = new ArrayList<>();
        type.add("1");
        type.add("1");
        type.add("1");
        type.add("1");
        HomeTaskTypeAdapter taskTypeAdapter = new HomeTaskTypeAdapter(R.layout.item_home_type, type);
        listTaskType.setAdapter(taskTypeAdapter);

        HomeTaskTypeAdapter taskAdAdapter = new HomeTaskTypeAdapter(R.layout.item_home_ad, type);
        ad.setAdapter(taskAdAdapter);

        homeAdapter.addHeaderView(headerView);
        listHome.setAdapter(homeAdapter);

        listHome.setPullRefreshListener(new PulltoRefreshRecyclerView.RecyPtrHandler() {
            @Override
            public void onLoadMore() {


            }

            @Override
            public void onRefresh() {

                listHome.refreshComplete();
            }
        });
        listHome.loadMoreEnd();
        return view;
    }
}
